package service.impl;

import dao.*;
import model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import service.ExcelService;
import util.ReturnResultUtil;
import util.UUIDGenerator;
import view.ImportView;
import view.QueryView;

import java.util.List;

/**
 * Created by gable on 2018/5/23.
 */
@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    ManningQuotasDao manningQuotasDao; //人员编制

    @Autowired
    PersonnelPerformanceDao personnelPerformanceDao; //人员绩效

    @Autowired
    MedicalBalanceDao medicalBalanceDao;  //收支情况

    @Autowired
    MedicalBusinessDao medicalBusinessDao; //医疗业务

    @Autowired
    RunningGuaranteeDao runningGuaranteeDao; //运行保障

    @Autowired
    FamilyPlanDao familyPlanDao;            //计生服务

    @Autowired
    GeneralDao generalDao;                  //全科医生

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResultUtil save(QueryView queryView, List<ImportView> list) {
        for(ImportView importView : list){
            //人员编制
            ManningQuotas manningQuotas = new ManningQuotas();
            if(!StringUtils.isEmpty(importView.getStaffNum())){  //编制数
                manningQuotas.setStaffNum(Integer.valueOf(importView.getStaffNum()));
            }
            if(!StringUtils.isEmpty(importView.getPermanentStaff())){  //在编人数
                manningQuotas.setPermanentStaff(Integer.valueOf(importView.getPermanentStaff()));
            }
            if(!StringUtils.isEmpty(importView.getNonStaff())){        //非在编人数
                manningQuotas.setNonStaff(Integer.valueOf(importView.getNonStaff()));
            }
            manningQuotas.setId(UUIDGenerator.getUniqueLong());
            manningQuotas.setHospitalId(importView.getHospitalId());
            manningQuotas.setMonth(queryView.getMonth());
            manningQuotas.setYear(queryView.getYear());
            manningQuotasDao.insert(manningQuotas);
            //人员绩效
            PersonnelPerformance pp = new PersonnelPerformance();
            pp.setId(UUIDGenerator.getUniqueLong());
            pp.setHospitalId(importView.getHospitalId());
            pp.setMonth(queryView.getMonth());
            pp.setYear(queryView.getYear());
            if(!StringUtils.isEmpty(importView.getStaffAnnualExpectedWage())) { //在编人均绩效工资年度期望值(元)
                pp.setStaffAnnualExpectedWage(Double.valueOf(importView.getStaffAnnualExpectedWage()));
            }
            if(!StringUtils.isEmpty(importView.getStaffPerCapitaWage())) {      //在编人均绩效工资(元)
                pp.setStaffPerCapitaWage(Double.valueOf(importView.getStaffPerCapitaWage()));
            }
            if(!StringUtils.isEmpty(importView.getStaffWageImplementationRate())) {      //在编人均绩效工资执行率(%)
                pp.setStaffWageImplementationRate(Double.valueOf(importView.getStaffWageImplementationRate()));
            }
            if(!StringUtils.isEmpty(importView.getAnnualExpectedHumanCost())) {      //人力成本总额年度期望值(万元)
                pp.setAnnualExpectedHumanCost(Double.valueOf(importView.getAnnualExpectedHumanCost()));
            }
            if(!StringUtils.isEmpty(importView.getHumanCost())) {      //人力成本总额(万元)
                pp.setHumanCost(Double.valueOf(importView.getHumanCost()));
            }
            if(!StringUtils.isEmpty(importView.getHumanCostImplementationRate())) {      //人力成本预算执行率(%)
                pp.setHumanCostImplementationRate(Double.valueOf(importView.getHumanCostImplementationRate()));
            }
            personnelPerformanceDao.insert(pp);
            //收支情况
            MedicalBalance mb = new MedicalBalance();
            mb.setId(UUIDGenerator.getUniqueLong());
            mb.setHospitalId(importView.getHospitalId());
            mb.setMonth(queryView.getMonth());
            mb.setYear(queryView.getYear());
            if(!StringUtils.isEmpty(importView.getMedicalIncome())) {      //医疗收入(万元)
                mb.setMedicalIncome(Double.valueOf(importView.getMedicalIncome()));
            }
            if(!StringUtils.isEmpty(importView.getMedicareIncome())) {      //医保病人收入(万元)
                mb.setMedicareIncome(Double.valueOf(importView.getMedicareIncome()));
            }
            if(!StringUtils.isEmpty(importView.getDrugIncome())) {      //药品收入(万元)
                mb.setDrugIncome(Double.valueOf(importView.getDrugIncome()));
            }
            if(!StringUtils.isEmpty(importView.getMedicalExpenditure())) {      //医疗业务支出(万元)
                mb.setMedicalExpenditure(Double.valueOf(importView.getMedicalExpenditure()));
            }
            if(!StringUtils.isEmpty(importView.getHumanExpenditure())) {      //人员支出(万元)
                mb.setHumanExpenditure(Double.valueOf(importView.getHumanExpenditure()));
            }
            if(!StringUtils.isEmpty(importView.getDrugExpenditure())) {      //药品支出(万元)
                mb.setDrugExpenditure(Double.valueOf(importView.getDrugExpenditure()));
            }
            medicalBalanceDao.insert(mb);
            //医疗业务
            MedicalBusiness mbu = new MedicalBusiness();
            mbu.setId(UUIDGenerator.getUniqueLong());
            mbu.setHospitalId(importView.getHospitalId());
            mbu.setMonth(queryView.getMonth());
            mbu.setYear(queryView.getYear());
            if(!StringUtils.isEmpty(importView.getOutpatientPersontime())) {      //门急诊人次(人次)
                mbu.setOutpatientPersontime(Integer.valueOf(importView.getOutpatientPersontime()));
            }
            if(!StringUtils.isEmpty(importView.getOutpatientReturnRate())) {      //门急诊复诊率
                mbu.setOutpatientReturnRate(Double.valueOf(importView.getOutpatientReturnRate()));
            }
            if(!StringUtils.isEmpty(importView.getOutpatientPerCapitaCost())) {      //门急诊人月均费用(元)
                mbu.setOutpatientPerCapitaCost(Double.valueOf(importView.getOutpatientPerCapitaCost()));
            }
            if(!StringUtils.isEmpty(importView.getOutpatientMedicineRate())) {      //门诊药占比(%)
                mbu.setOutpatientMedicineRate(Double.valueOf(importView.getOutpatientMedicineRate()));
            }
            if(!StringUtils.isEmpty(importView.getDischarges())) {      //出院人次
                mbu.setDischarges(Integer.valueOf(importView.getDischarges()));
            }
            if(!StringUtils.isEmpty(importView.getHospitalizationMedicineRate())) {      //住院药占比(%)
                mbu.setHospitalizationMedicineRate(Double.valueOf(importView.getHospitalizationMedicineRate()));
            }
            if(!StringUtils.isEmpty(importView.getMedicineRate())) {      //药品收入占医疗收入比（%）
                mbu.setMedicineRate(Double.valueOf(importView.getMedicineRate()));
            }
            if(!StringUtils.isEmpty(importView.getHygienicMaterialRate())) {      //非药品收入中卫材比(%)
                mbu.setHygienicMaterialRate(Double.valueOf(importView.getHygienicMaterialRate()));
            }
            if(!StringUtils.isEmpty(importView.getMedicalEvent())) {      //医疗事件
                mbu.setMedicalEvent(Integer.valueOf(importView.getMedicalEvent()));
            }
            if(!StringUtils.isEmpty(importView.getAdministrativePenaltyCases())) {      //行政处罚案件数
                mbu.setAdministrativePenaltyCases(Integer.valueOf(importView.getAdministrativePenaltyCases()));
            }
            if(!StringUtils.isEmpty(importView.getAverageStayLength())) {      //平均住院日（日）
                mbu.setAverageStayLength(Double.valueOf(importView.getAverageStayLength()));
            }
            if(!StringUtils.isEmpty(importView.getAverageHospitalizationCost())) {      //住院均次费用（万元）
                mbu.setAverageHospitalizationCost(Double.valueOf(importView.getAverageHospitalizationCost()));
            }
            if(!StringUtils.isEmpty(importView.getLevel3Level4OperationRate())) {      //三、四级手术百分率（%）
                mbu.setLevel3Level4OperationRate(Double.valueOf(importView.getLevel3Level4OperationRate()));
            }
            if(!StringUtils.isEmpty(importView.getSelectiveOperationRate())) {      //择期手术百分率(%)
                mbu.setSelectiveOperationRate(Double.valueOf(importView.getSelectiveOperationRate()));
            }
            if(!StringUtils.isEmpty(importView.getBloodPressure())) {      //35岁以上人群首诊测压人数
                mbu.setBloodPressure(Integer.valueOf(importView.getBloodPressure()));
            }
            if(!StringUtils.isEmpty(importView.getFeverClinicPersontime())) {      //发热门诊就诊人次数
                mbu.setFeverClinicPersontime(Integer.valueOf(importView.getFeverClinicPersontime()));
            }
            if(!StringUtils.isEmpty(importView.getActualSettlement())) {      //实际结算床日数
                mbu.setActualSettlement(Double.valueOf(importView.getActualSettlement()));
            }
            if(!StringUtils.isEmpty(importView.getBrithSexRate())) {      //出生性别比
                mbu.setBrithSexRate(Double.valueOf(importView.getBrithSexRate()));
            }
            medicalBusinessDao.insert(mbu);
            //运行保障
            RunningGuarantee rg = new RunningGuarantee();
            rg.setId(UUIDGenerator.getUniqueLong());
            rg.setHospitalId(importView.getHospitalId());
            rg.setMonth(queryView.getMonth());
            rg.setYear(queryView.getYear());
            if(!StringUtils.isEmpty(importView.getSafetyAccident())) {      //安全生产事故发生数
                rg.setSafetyAccident(Integer.valueOf(importView.getSafetyAccident()));
            }
            if(!StringUtils.isEmpty(importView.getSmallProjectPractice())) {      //限额以下小型建设项目实施数
                rg.setSmallProjectPractice(Integer.valueOf(importView.getSmallProjectPractice()));
            }
            runningGuaranteeDao.insert(rg);
            //计生服务
            FamilyPlan fp = new FamilyPlan();
            fp.setId(UUIDGenerator.getUniqueLong());
            fp.setHospitalId(importView.getHospitalId());
            fp.setMonth(queryView.getMonth());
            fp.setYear(queryView.getYear());
            if(!StringUtils.isEmpty(importView.getMidtermAbortion())) {      //中期引产数（例）
                fp.setMidtermAbortion(Integer.valueOf(importView.getMidtermAbortion()));
            }
            if(!StringUtils.isEmpty(importView.getMidtermAbortionSexRate())) {      //中期引产性别比（%）
                fp.setMidtermAbortionSexRate(Double.valueOf(importView.getMidtermAbortionSexRate()));
            }
            if(!StringUtils.isEmpty(importView.getBrithSexRate())) {      //出生性别比
                fp.setBrithSexRate(Double.valueOf(importView.getBrithSexRate()));
            }
            familyPlanDao.insert(fp);
            //全科医生
            General general = new General();
            general.setId(UUIDGenerator.getUniqueLong());
            general.setHospitalId(importView.getHospitalId());
            general.setMonth(queryView.getMonth());
            general.setYear(queryView.getYear());
            if(!StringUtils.isEmpty(importView.getActual())) {      //全科医生
                general.setActual(Integer.valueOf(importView.getActual()));
            }
            if(!StringUtils.isEmpty(importView.getActualRate())) {      //千人口全科医生数
                general.setActualRate(Double.valueOf(importView.getActualRate()));
            }
            generalDao.insert(general);
        }
        return new ReturnResultUtil(1,"保存成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResultUtil deleteRepeatData(QueryView queryView) {
        manningQuotasDao.deleteRepeatData(queryView);
        personnelPerformanceDao.deleteRepeatData(queryView);
        medicalBalanceDao.deleteRepeatData(queryView);
        medicalBusinessDao.deleteRepeatData(queryView);
        runningGuaranteeDao.deleteRepeatData(queryView);
        familyPlanDao.deleteRepeatData(queryView);
        generalDao.deleteRepeatData(queryView);
        return new ReturnResultUtil(0,"删除成功");
    }

}
